using System;
using Lottery.App.DAL;
using System.Text.RegularExpressions;
using Lottery.App.Model;
using Lottery.App.Core;

namespace Lottery.App.Test.Core
{
    public class Tool
    {
        private RecordDao dao = new RecordDao();
        private WebUtils web = new WebUtils();

        public void GetListPage(int page)
        {
            string url = String.Format("http://www.hnflcp.com/newstypelist.asp?pageno={0}&intype=71", page);

            string result = web.DoGet(url, null);

            Regex reg = new Regex(@"nid=(\d+)'>快乐十分之“动物总动员”(\d+)月(\d+)日开奖号码公布");

            MatchCollection mc = reg.Matches(result);

            int month, day;

            for (int i = mc.Count - 1; i >= 0; i--)
            {
                month = Convert.ToInt32(mc[i].Groups[2].Value);
                day = Convert.ToInt32(mc[i].Groups[3].Value);

                GetShowPage(mc[i].Groups[1].Value, String.Format("2012{0:D2}{1:D2}", month, day));
            }
        }

        public void GetShowPage(string nid, string dt)
        {
            string url = String.Format("http://www.hnflcp.com/newsshow.asp?nid={0}", nid);

            string result = web.DoGet(url, null);

            int startIndex = result.IndexOf("销售期号");
            int endIndex = result.IndexOf("关闭窗口");

            result = result.Substring(startIndex, endIndex - startIndex);

            Regex reg = new Regex(@">(\d{1,2})<");

            MatchCollection mc = reg.Matches(result);

            //if (mc.Count == 0 || mc.Count != 672) { }

            int num = 1;
            string str = String.Empty;
            RecordInfo r = null;

            dao.BeginTransaction();

            int count = 1;
            for (int i = 0; i < mc.Count; i++)
            {
                str += String.Format("{0:D2} ", mc[i].Groups[1].Value);

                if (count++ % 8 == 0)
                {
                    r = new RecordInfo(String.Format("{0}{1:D3}", dt, num), str.Trim());
                    
                    str = String.Empty;
                    count = 1;
                    num++;

                    dao.Save(r);
                }
            }

            dao.CommitTransaction();
        }

    }
}
